<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2023/12/26
 * Time: 17:04
 */

namespace app\api\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;

class Shiju extends Command
{

    protected function configure()
    {
        $this->setName('shiju')              		//定义命令的名字
        ->setDescription('shiju html')      //定义命令的描述
        ->addArgument('method')                   //参数1
        ->addArgument('url')                   //文章url
        ->addArgument('id');                    //文章id
    }

    //php think shiju update_show(publish)
    protected function execute(Input $input, Output $output)
    {

        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义

        $method = $input->getArgument('method');
        if($method == 'update_show'){
            $this->update_show();
        } else if($method == 'update_status'){
            $this->update_status();
        }
    }

    private function update_show(){
        $this->ticheck();
        $model = Model('app\common\model\Shiju');
        $rs = $model->field("id,title")->where(['status'=>1,'is_show'=>0])->order('id asc')->limit(3)->select();
        if(!empty($rs)){
            foreach($rs as $k=>$v){
                $model->where(['id'=>$v['id']])->update(['is_show'=>1]);
                echo $model->getLastSql()."\r\n";
            }
        }
    }
    protected function ticheck(){
        $nowH = (int)date("H",time());
        if($nowH < 8 || $nowH > 18){
            echo "不在提交时间内！";exit;
        }
    }


    private function update_status(){
        $model = Model('app\common\model\Shiju');
        $poetryModel = Model('app\common\model\Poetry');
        $poetModel = Model('app\common\model\Poet');
        $rs = $model->where(['poetry_id'=>'-1'])->limit(30)->order('id asc')->select()->toArray();
        echo $model->getLastSql()."\r\n";
        if(!empty($rs)){
            foreach($rs as $k=>$v){
                $updateSave = [];
                if(!empty($v['poetry_name']) && !empty($v['poet_name'])){
                    $poetryData = $poetryModel->where(['title'=>$v['poetry_name'],'author'=>$v['poet_name']])->find();
                    echo $poetryModel->getLastSql()."\r\n";
                    if(!empty($poetryData)){
                        $updateSave['poetry_id'] = $poetryData['id'];
                        if(!empty($poetryData['author_id'])){
                            $poetData = $poetModel->where(['id'=>$poetryData['author_id']])->find();
                        } else if(!empty($poetryData['author'])){
                            $poetData = $poetModel->where(['id'=>$poetryData['author']])->find();
                        }
                        if(!empty($poetData)){
                            if(isset($poetData['id'])){
                                $updateSave['poet_id'] = $poetData['id'];
                            }
                            if(isset($poetData['name'])){
                                $updateSave['poet_name'] = $poetData['name'];
                            }
                            if(isset($poetData['dynasty'])){
                                $updateSave['dynasty_name'] = $poetData['dynasty'];
                            }
                            if(isset($poetData['dynasty_id'])){
                                $updateSave['dynasty_id'] = $poetData['dynasty_id'];
                            }
                        }
                        if(!empty($updateSave)){
                            echo $v['id']."=>".$v['title']."=>".$v['poetry_name']."\r\n";
                            print_r($updateSave);
                        }

                    }
                }

            }
        }
    }
}